package file.listener;

import file.FileFrame;
import file.handle.FilePannel;
import file.handle.impl.FilePannelImpl;
import file.model.FileModel;
import file.model.ImageModel;
import file.model.SearchModel;

import javax.swing.*;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.io.File;
import java.util.ArrayList;
import java.util.List;

/**
 * 搜索按钮监听器
 * @Author: zou
 * @email: 704572528@qq.com
 * @Date: 2020/10/27 18:44
 */
public class SearchBtnListener extends FileFrameAbstract implements MouseListener {

    private FilePannel filePannel = new FilePannelImpl(fileFrame);

    /**
     * 搜索模型
     */
    private SearchModel searchModel;

    /**
     * 获取文件的对象
     */
    private List<FileModel> dataModel;

    public SearchBtnListener(FileFrame fileFrame) {
        super(fileFrame);
    }


    /**
     * 重写鼠标点解
     * @param e
     */
    @Override
    public void mouseClicked(MouseEvent e) {
        // 获取此路径下的所有文件
        File file = new File(getPathLable());
        if (file != null) {
            dataModel = new ArrayList<>();
            // 匹对文件
            matchinFile(file);
            // 更新Ui
            updateUi();
        }
    }

    /**
     * 更新UI
     */
    private void updateUi() {
        JPanel filePanel = this.fileFrame.getFilePanel();
        // 清除布局
        filePannel.removeFile(filePanel);
        if (dataModel != null) {

            dataModel.forEach(d -> {
                filePannel.addFilePanel(d,filePanel,new ImageModel(FilePannelImpl.fileImage, FilePannelImpl.folderImage));
            });

            // 更新Ui
            filePannel.updateUI(filePanel);
        }

    }


    @Override
    public void mousePressed(MouseEvent e) {

    }

    @Override
    public void mouseReleased(MouseEvent e) {

    }

    @Override
    public void mouseEntered(MouseEvent e) {

    }

    @Override
    public void mouseExited(MouseEvent e) {

    }

    /**
     * 匹对文件
     * @param files
     */
    private void matchinFile(File files) {
        // 搜索文件名
        String text = fileFrame.getSearchName().getText();
        String name = files.getName();
        if (files != null) {
            if (files.isFile()) {
                dataModelJoin(text, name, files, false);
            } else {
                dataModelJoin(text, name, files, true);
                File[] files1 = files.listFiles();
                if (files1 != null) {
                    for (File file : files1) {
                        boolean isFolder = false;
                        if (!file.isFile()) {
                            isFolder = true;
                            // 递归进去
                            matchinFile(file);
                        }
                        dataModelJoin(text, name, file, isFolder);
                    }
                }
            }
        }
    }

    private void dataModelJoin(String text, String name, File file, boolean isFolder) {
        if (name.contains(text)) {
            // 文件列表添加
            dataModel.add(new FileModel(name, file.getPath(), false, isFolder));
        }
    }

    public List<FileModel> getDataModel() {
        return dataModel;
    }

    public void setDataModel(List<FileModel> dataModel) {
        this.dataModel = dataModel;
    }
}
